package cn.timelost.hr.controller;

import cn.timelost.hr.config.utils.JWTUtils;
import cn.timelost.hr.config.utils.SysTime;
import cn.timelost.hr.dao.AttendanceDao;
import cn.timelost.hr.dao.PersonalDao;
import cn.timelost.hr.enums.ResultEnum;
import cn.timelost.hr.pojo.*;
import cn.timelost.hr.service.AttendanceService;
import cn.timelost.hr.service.UserService;
import cn.timelost.hr.vo.ResultVo;
import cn.timelost.hr.vo.UnAttendanceVo;
import cn.timelost.hr.vo.input.AttendanceForm;
import cn.timelost.hr.vo.input.PersonalRewardForm;
import com.github.pagehelper.PageInfo;
import lombok.Data;
import org.apache.ibatis.jdbc.Null;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.sql.Date;
import java.util.List;

/**
 * 签到接口
 * @auther LiuXinqi
 *
 */
@RestController//响应返回全json
@RequestMapping("/attendance")
public class AttendanceController {

    @Resource
    AttendanceService attendanceService;
    @Resource
    AttendanceDao attendanceDao;
    @Resource
    PersonalDao personalDao;
    @Autowired
    UserService userService;



    /**
     * 正常全表=单查询功能
     */
    @GetMapping("/attendance/list")
    public PageInfo<Attendance> list(@RequestParam(value = "page", defaultValue = "1") Integer page,
                                     @RequestParam(value = "size", defaultValue = "10") Integer size,
                                     @RequestParam(value = "personalId", defaultValue = "0") Integer personalId,
                                     @RequestParam(value = "beginDate", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") java.util.Date beginDate,
                                     @RequestParam(value = "endDate", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") java.util.Date endDate
    ) {
        return attendanceService.findAll(page, size, personalId, new SysTime().getTurnDate(beginDate) , new SysTime().getTurnDate(endDate));
    }
//    @GetMapping("/attendance/list")
//    public PageInfo<Attendance2> list(@RequestParam(value = "page", defaultValue = "1") Integer page,
//                                     @RequestParam(value = "size", defaultValue = "10") Integer size,
//                                     @RequestParam(value = "personalId", defaultValue = "0") Integer personalId,
//                                     @RequestParam(value = "beginDate", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") java.util.Date beginDate,
//                                     @RequestParam(value = "endDate", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") java.util.Date endDate
//    ) {
//        System.out.println("Hello08"+personalId);
//        return attendanceService.findAllView(page, size, personalId, new SysTime().getTurnDate(beginDate) , new SysTime().getTurnDate(endDate));
//    }
    @GetMapping("/attendance/list2")
    public PageInfo<Attendance2> list2(@RequestParam(value = "page", defaultValue = "1") Integer page,
                                      @RequestParam(value = "size", defaultValue = "10") Integer size,
                                      @RequestParam(value = "personalId", defaultValue = "0") Integer personalId,
                                      @RequestParam(value = "beginDate", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") java.util.Date beginDate,
                                      @RequestParam(value = "endDate", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") java.util.Date endDate
    ) {
//        System.out.println();
        System.out.println("Hello08881"+attendanceService.findUnAttendanceAll(page, size));
//        System.out.println("Hello0888811"+attendanceService.findAllView(page, size, personalId, new SysTime().getTurnDate(beginDate) , new SysTime().getTurnDate(endDate)));


        return attendanceService.findAllView(page, size, personalId, new SysTime().getTurnDate(beginDate) , new SysTime().getTurnDate(endDate));
    }

    @GetMapping("/attendance/listUnattendance")
    public PageInfo<UnAttendanceVo> listUnattendance(@RequestParam(value = "page", defaultValue = "1") Integer page,
                                                     @RequestParam(value = "size", defaultValue = "10") Integer size
    ) {
        return attendanceService.findUnAttendanceAll(page, size);
    }



    @GetMapping("/attendance/all")
    public List<Attendance> all() {
        return attendanceService.all();
    }
    @GetMapping("/attendance/all2")
    public List<Attendance2> allView() {
        return attendanceService.allView();
    }

    @GetMapping("/attendance/{id}")
    public Attendance findById(@PathVariable Integer id) {
        return attendanceService.find(id);
    }



    /**
     * 和PersonReword的find(id)返回类型一样，和PersonReword的insert(Form)参数类型类似
     * @param attendanceForm
     * @return
     */
    @GetMapping("/get")
//    public Attendance signUp(@RequestBody @Valid AttendanceForm attendanceForm){
    public Attendance signUp(@RequestBody @Valid AttendanceForm attendanceForm){
        if(attendanceForm==null)return null;
        if(attendanceForm.getPersonalId()==null)return null;
        System.out.println("hello09"+attendanceForm.getPersonalId());
        return attendanceService.signUp(attendanceForm.getPersonalId());
    }

    @PostMapping("/insert")
    public ResultVo insert(@RequestBody @Valid AttendanceForm attendanceForm) {

        Attendance attendance=attendanceService.signUp(attendanceForm.getPersonalId());
        try{
            attendanceDao.insert(attendance);
            return ResultVo.success();
        }catch (Exception e){
            return ResultVo.fail(ResultEnum.ATTENDANCE_EXIST);
        }
    }

    /**
     * insert2
     * 无参签到，但需要员工登录
     * @param httpServletRequest
     * @return
     */
    @PostMapping("/insertAuto")
    public ResultVo insertAuto(HttpServletRequest httpServletRequest) {
        System.out.println("hello4");
        String token = httpServletRequest.getHeader("Authorization");
        String username = JWTUtils.getUsername(token);
        User user=userService.findByUsername(username);
        Date date= new SysTime().getDate();

        try{
            System.out.println("Hello4"+user.getId());
            Attendance attendance=attendanceDao.getStatusInt(user.getId(),date);
            System.out.println("Hello4"+attendance.getStatus());
//            return attendance.getStatus();
        }catch (Exception e){
            System.out.println("Hello4"+"-1");
//            return -1;//没查到
        }
//        String token = httpServletRequest.getHeader("Authorization");
//        String username = JWTUtils.getUsername(token);
//        User user=userService.findByUsername(username);

//        Attendance attendance=attendanceService.signUp(user.getId());
        System.out.println("signat"+user.getUsername()+" "+personalDao.findAllByName(user.getUsername()));

        try{
            attendanceService.signUp(personalDao.findAllByName(user.getUsername()));
            return ResultVo.success();
        }catch (Exception e){
            return ResultVo.fail(ResultEnum.ATTENDANCE_EXIST);
        }
    }
    /**
     * insertUn
     * 补签，需要传入员工id，但需要在未签到表单使用
     * @param id
     * @return
     */
    @PostMapping("/insertUn")
    public ResultVo insertUn(Integer id) {
//        System.out.println("hello4");
//        String token = httpServletRequest.getHeader("Authorization");
//        String username = JWTUtils.getUsername(token);
//        User user=userService.findByUsername(username);
//        Date date= new SysTime().getDate();

//        try{
//            System.out.println("Hello4"+id);
//            Attendance attendance=attendanceDao.getStatusInt(id,date);
//            System.out.println("Hello4"+attendance.getStatus());
////            return attendance.getStatus();
//        }catch (Exception e){
//            System.out.println("Hello4"+"-1");
////            return -1;//没查到
//        }
//        String token = httpServletRequest.getHeader("Authorization");
//        String username = JWTUtils.getUsername(token);
//        User user=userService.findByUsername(username);
//        Attendance attendance=attendanceService.signUp(user.getId());
//        System.out.println("signat"+user.getUsername()+" "+personalDao.findAllByName(user.getUsername()));

        try{
            attendanceService.signUpUn(id);
            return ResultVo.success();
        }catch (Exception e){
            return ResultVo.fail(ResultEnum.ATTENDANCE_EXIST);
        }
    }

    /**
     * 获取签到情况
     * @param httpServletRequest
     * @return
     */
    @GetMapping("/getAttendanceStatus")
    public int findStatus(HttpServletRequest httpServletRequest){

        //测试其它功能
        System.out.println("HelloW"+attendanceDao.getAttendance0());
        System.out.println("HelloW"+attendanceDao.getAttendance1());
        System.out.println("HelloW"+attendanceDao.getAttendance2());
        System.out.println("HelloW"+attendanceDao.getAttendanceS());

        //
        String token = httpServletRequest.getHeader("Authorization");
        String username = JWTUtils.getUsername(token);
        User user=userService.findByUsername(username);
        Date date= new SysTime().getDate();
        System.out.println("HelloW1"+personalDao.findAllByName(username));
        try{
            Attendance attendance=attendanceDao.getStatusInt(personalDao.findAllByName(username),date);
            System.out.println("Hello40"+attendance.getStatus());
            return attendance.getStatus();
        }catch (Exception e){
            return -1;//没查到
        }
    }
    /**
     * 获取今日签到各个情况人数,0已经签到1迟到2旷工3未签
     */
    @GetMapping("/getAttendanceStatus0")
    public int getAttendance0(HttpServletRequest httpServletRequest){
        return attendanceDao.getAttendance0();
    }
    @GetMapping("/getAttendanceStatus1")
    public int getAttendance1(HttpServletRequest httpServletRequest){
        return attendanceDao.getAttendance1();
    }
    @GetMapping("/getAttendanceStatus2")
    public int getAttendance2(HttpServletRequest httpServletRequest){
        return attendanceDao.getAttendance2();
    }
    @GetMapping("/getAttendanceStatusS")
    public int getAttendanceS(HttpServletRequest httpServletRequest){
        return attendanceDao.getAttendanceS();
    }

    @GetMapping("/getAttendanceStatusAllnum")
    public int[] getAttendanceAllnum(HttpServletRequest httpServletRequest){
        int num[]=new int[4];
        num[0]=attendanceDao.getAttendance0();;
        num[1]=attendanceDao.getAttendance1();;
        num[2]=attendanceDao.getAttendance2();;
        num[3]=attendanceDao.getAttendanceS();;
        return num;
    }

}
